﻿/// <reference path="jquery-1.7.2.js" />


(function ($) {
    $.fn.SparrowMenu = function (options) {
        var defaults =
        {
            LinkHoverClass: "",
            LinkSelectedClass: "",
            MainItem: "MainItem",
            SubItem: "SubItem",
            ItemClick: function (item) {
            }
        };

        $.extend(defaults, options);
        var selectItem;

        var mainItems = $(this).find("li[ItemType=" + defaults.MainItem + "]");
        mainItems.find(".m-link").bind("click", function () {
            $(this).parent().find("ul").toggle();
        });

        mainItems.find("ul").toggle(false);

        mainItems.find("span:first").hover(function () {
            $(this).addClass(defaults.LinkHoverClass);
        }, function () {
            $(this).removeClass(defaults.LinkHoverClass);
        });
        //add sub item click behavior
        var subItems = $(mainItems).find("li");
        subItems.hover(
                    function () {
                        if (selectItem == null || selectItem != this) {
                            $(this).addClass(defaults.LinkHoverClass);
                            var url = $(this).attr("url");
                            window.status = url;
                        }
                    },
                    function () {
                        $(this).removeClass(defaults.LinkHoverClass)
                        window.status = "";
                    });
        subItems.click(function () {
            if (selectItem != null && selectItem != this) {
                $(selectItem).removeClass(defaults.LinkSelectedClass);
            }
            else {
                $("."+defaults.LinkSelectedClass).removeClass(defaults.LinkSelectedClass); 
            }
            $(this).removeClass(defaults.LinkHoverClass);
            $(this).addClass(defaults.LinkSelectedClass);
            selectItem = this;
            var clickItem = this;
            clickItem.Text = $(this).text();
            defaults.ItemClick(clickItem)
        });
        var g = {
            IsExpland: function (isShow) {
                g.menu.find("ul").toggle(isShow);
            }
        };
        g.menu = mainItems;
        return g;

    };
})(jQuery);





